﻿using System;
using Sunny.UI;
using ThermalImagery.Comm;
using ThermalImagery.Business;
using LeederGlobal;
using System.Drawing;

namespace ThermalImagery.Forms
{
    public partial class FrmSetup : UIPage
    {
        SetupBLL setupBLL = new SetupBLL();
        bool isLoad = true;      //判断窗体是否首次加载
        public FrmSetup()
        {
            InitializeComponent();
            GolbalVariable.SysSetup = setupBLL.GetModel();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission != 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            GolbalVariable.SysSetup.PortName= cbPort.SelectedItem.ToString();
            GolbalVariable.SysSetup.OkCount= IntOkCount.Value;
            GolbalVariable.SysSetup.OkDifferenceValue = IntOkDifferenceValue.Value;
            GolbalVariable.SysSetup.OkValue= IntOkValue.Value;
            GolbalVariable.SysSetup.CheckTime= IntCheckTime.Value;
            GolbalVariable.SysSetup.PhotoelectricityInput= int.Parse(txtPhotoelectricityInput.Text);
            GolbalVariable.SysSetup.SwitchInput= int.Parse(txtSwitchInput.Text);
            GolbalVariable.SysSetup.RedLampOutput= int.Parse(txtRedLampOutput.Text);
            GolbalVariable.SysSetup.GreenLampOutput= int.Parse(txtGreenLampOutput.Text);
            GolbalVariable.SysSetup.BuzzerOutput= int.Parse(txtBuzzerOutput.Text);
            GolbalVariable.SysSetup.PhotoelectricityDelay = intPhotoelectricityDelay.Value;
            GolbalVariable.SysSetup.CameraIP = txtCameraIP.Text;
            GolbalVariable.SysSetup.CameraPort = int.Parse(txtCameraPort.Text);
            GolbalVariable.SysSetup.CameraUserId = txtCameraUserId.Text;
            GolbalVariable.SysSetup.CameraPwd = txtCameraPwd.Text;
            GolbalVariable.SysSetup.PirtureRetentionDuration = int.Parse(txtPirtureRetentionDuration.Text);
            GolbalVariable.SysSetup.PirtureClearTime = int.Parse(cbPirtureClearTime.SelectedItem.ToString());
            GolbalVariable.SysSetup.DetectionMode = cbDetectionMode.SelectedIndex +1;
            setupBLL.Update(GolbalVariable.SysSetup);
            UIMessageTip.ShowOk("保存成功！");
        }

        private void FrmSetup_Load(object sender, EventArgs e)
        {
            //如果窗体已经加载过则不重新加载
            if (!isLoad)
                return;
            isLoad = false;
            uiLabel15.ForeColor = Color.Red;
            cbPort.SelectedItem = GolbalVariable.SysSetup.PortName;
            IntOkCount.Value = GolbalVariable.SysSetup.OkCount;
            IntOkDifferenceValue.Value = GolbalVariable.SysSetup.OkDifferenceValue;
            IntOkValue.Value = GolbalVariable.SysSetup.OkValue;
            IntCheckTime.Value = GolbalVariable.SysSetup.CheckTime;
            txtPhotoelectricityInput.Text = GolbalVariable.SysSetup.PhotoelectricityInput.ToString();
            txtSwitchInput.Text = GolbalVariable.SysSetup.SwitchInput.ToString();
            txtRedLampOutput.Text = GolbalVariable.SysSetup.RedLampOutput.ToString();
            txtGreenLampOutput.Text = GolbalVariable.SysSetup.GreenLampOutput.ToString();
            txtBuzzerOutput.Text = GolbalVariable.SysSetup.BuzzerOutput.ToString();
            intPhotoelectricityDelay.Value = GolbalVariable.SysSetup.PhotoelectricityDelay;
            txtCameraIP.Text = GolbalVariable.SysSetup.CameraIP.ToString();
            txtCameraPort.Text = GolbalVariable.SysSetup.CameraPort.ToString();
            txtCameraUserId.Text = GolbalVariable.SysSetup.CameraUserId.ToString();
            txtCameraPwd.Text = GolbalVariable.SysSetup.CameraPwd.ToString();
            txtPirtureRetentionDuration.Text = GolbalVariable.SysSetup.PirtureRetentionDuration.ToString();
            cbPirtureClearTime.SelectedItem = GolbalVariable.SysSetup.PirtureClearTime.ToString();
            cbDetectionMode.SelectedIndex = GolbalVariable.SysSetup.DetectionMode - 1;
        }
    }
}
